﻿#define _CRT_SECURE_NO_WARNINGS 1

//904. ⽔果成篮
class Solution {
public:
    int totalFruit(vector<int>& fruits) {

        //unordered_map<int,int> mp;
        int hash[100000] = { 0 };
        int len = 0, kinds = 0;
        for (int left = 0, right = 0; right < fruits.size(); ++right)
        {
            if (hash[fruits[right]] == 0)
                kinds++;
            //进窗口 
            hash[fruits[right]]++;
            //判断
            while (kinds > 2)
            {
                //出窗口
                hash[fruits[left]]--;
                if (hash[fruits[left]] == 0)
                    --kinds;
                ++left;
            }
            //更新结果
            len = max(len, right - left + 1);
        }
        return len;

    }
};